﻿namespace PayPal.AdaptivePayments
{
    using PayPal.Platform.SDK;

    public static class RefundTransaction
    {
        public static RefundResponse Execute(string transactionId, string language = "")
        {
            if (string.IsNullOrEmpty(language))
            {
                language = Profile.Language;
            }

            Validate(transactionId, language);
            
            RefundRequest refundRequest = new RefundRequest();
            RefundResponse response = null;

            refundRequest.requestEnvelope = new RequestEnvelope();
            refundRequest.requestEnvelope.errorLanguage = language;
            refundRequest.transactionId = transactionId;

            AdapativePayments ap = new AdapativePayments();
            ap.APIProfile = Profile.FNProfile;
            response = ap.Refund(refundRequest);

            if (ap.LastError != null)
            {
                throw ap.LastError;
            }

            return response;       
        }

        private static void Validate(string transactionId, string language)
        {
            ValidationHelper.ValidateProfile(Profile.FNProfile);
            ValidationHelper.ValidateLanguage(language);
        }
    }
}
